package com.chilly.other.thread.treadclass;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * http://www.liuzk.com/183.html
 * 线程实现方式：FutureTask+Callable
 * Created by Chilly Cui on 2020/12/17.
 */
public class ThreadFutureDemo {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        FutureTask task = new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 0; i < 10; i++) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                    sum += i;
                }
                //注意看这里有返回值
                return sum;
            }
        });

        Thread instance = new Thread(task);
        instance.start();

        Thread.sleep(1000);

        System.out.println(task.get());

    }
}
